package com.pig4cloud.pig.module.controller.platform;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.core.util.RegexUtil;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.platform.PlatformHelp;
import com.pig4cloud.pig.module.service.platform.IPlatformHelpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 帮助文档Controller
 *
 * @author ruoyi
 * @date 2021-10-30
 */
@Validated
@Api(value = "帮助文档控制器", tags = {"帮助文档管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/platform/help")
public class PlatformHelpController {

    private final IPlatformHelpService platformHelpService;

    /**
     * 查询帮助文档列表
     */
    @ApiOperation("查询帮助文档列表")
    @PreAuthorize("@pms.hasPermission('module:platformhelp:list')")
    @GetMapping("/list")
    public TableDataInfo<PlatformHelp> list(@Validated PlatformHelp platformHelp) {
        QueryWrapper<PlatformHelp> qw = new QueryWrapper<>();
        if(platformHelp.getParentId() !=null){
        	qw.eq("parent_id",platformHelp.getParentId());
		}
		qw.orderByAsc("sort");
        TableDataInfo<PlatformHelp> td = PageUtils.buildDataInfo(platformHelpService.page(PageUtils.buildPage(),qw));
        List<PlatformHelp> list = td.getData();
        return td;
    }


	/**
	 * 获取部门下拉树列表
	 */
	@GetMapping("/treeSelect")
	public R treeselect(PlatformHelp dept) {
		List<PlatformHelp> eventTypes = platformHelpService.getNoPageByList(dept);
		return R.ok(platformHelpService.buildPlatformHelpTreeSelect(eventTypes));
	}


	/**
     * 导出帮助文档列表
     */
    @ApiOperation("导出帮助文档列表")
    @PreAuthorize("@pms.hasPermission('module:platformhelp:export')")
    @SysLog("帮助文档")
    @GetMapping("/export")
    public List export(@Validated PlatformHelp platformHelp) {
        List<PlatformHelp> list = platformHelpService.list();
        // ExcelUtil<PlatformHelp> util = new // ExcelUtil<>(PlatformHelp.class);
        return  list;// util.exportExcel(list, "帮助文档");
    }

    /**
     * 获取帮助文档详细信息
     */
    @ApiOperation("获取帮助文档详细信息")
    @PreAuthorize("@pms.hasPermission('module:platformhelp:query')")
    @GetMapping("/{id}")
    public R<PlatformHelp> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        PlatformHelp platformHelp = platformHelpService.getById(id);
        return R.ok(platformHelp);
    }

    /**
     * 新增帮助文档
     */
    @ApiOperation("新增帮助文档")
    @PreAuthorize("@pms.hasPermission('module:platformhelp:add')")
    @SysLog("帮助文档")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody PlatformHelp platformHelp) {

		platformHelp.setContent(RegexUtil.convertProductDescription(platformHelp.getContent()));
        return R.okByRow(platformHelpService.save(platformHelp) ? 1 : 0);
    }

    /**
     * 修改帮助文档
     */
    @ApiOperation("修改帮助文档")
    @PreAuthorize("@pms.hasPermission('module:platformhelp:edit')")
    @SysLog("帮助文档")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody PlatformHelp platformHelp) {
		platformHelp.setContent(RegexUtil.convertProductDescription(platformHelp.getContent()));
        return R.okByRow(platformHelpService.updateById(platformHelp) ? 1 : 0);
    }

    /**
     * 删除帮助文档
     */
    @ApiOperation("删除帮助文档")
    @PreAuthorize("@pms.hasPermission('module:platformhelp:remove')")
    @SysLog("帮助文档")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(platformHelpService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
